//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.core.UserConfirmCommandConfig;
	import reader.events.MediaEvent;
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.MediaVO;
	import reader.utils.DateRange;
	import reader.view.components.BlogWriter;
	import reader.view.components.MediaManager;

	public class MediaManagerMediator extends ServiceAbleMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "MediaManagerMediator";
		
		public function get mediaManager():MediaManager
		{
			return this.viewComponent as MediaManager;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var media:Vector.<MediaVO>;
		private var draftProxy:IDraftProxy;
		private var serviceProxy:IServiceProxy;
		
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function MediaManagerMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			startUp();
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.MEDIA_ALL,
									ApplicationFacade.WRITER_CLOSE,
									ApplicationFacade.WRITER_VIEW_CHANGE
									]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.MEDIA_ALL:
					media = notification.getBody() as Vector.<MediaVO>;
					mediaManager.mediaDataGrid = new ArrayCollection(MediaVO.VectorToArray(media));
					break;
				case ApplicationFacade.WRITER_CLOSE:
					clearReference();
					break;
				case ApplicationFacade.WRITER_VIEW_CHANGE:
					if(String(notification.getBody()) == BlogWriter.VIEW_MEDIA)
					{
						draftProxy.getAllMedia();
					} 
					break;
						
			}
		}
		
		override public function startUp():void
		{
			draftProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.draftProxyName) as IDraftProxy;
			serviceProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			
			mediaManager.addEventListener(FlexEvent.CREATION_COMPLETE, onCreateComplete_handler);
			mediaManager.addEventListener(MediaEvent.EDIT_MEDIA, onEditMedia_handler);
			mediaManager.addEventListener(MediaEvent.DELETE_MEDIA, onDeleteDraft_handler);
			mediaManager.addEventListener(MediaEvent.VIEW_MEDIA, onViewMediaDetail_handler);
			mediaManager.addEventListener(MediaEvent.DELETE_MEDIAS, onDeleteMedia_handler, false, 0, true);
		}
		
		override public function clearReference():void
		{
			readerModelLocator = null;
			draftProxy = null;
			serviceProxy = null;
			mediaManager.removeEventListener(MediaEvent.EDIT_MEDIA, onEditMedia_handler);
			mediaManager.removeEventListener(MediaEvent.DELETE_MEDIA, onDeleteDraft_handler);
			mediaManager.removeEventListener(MediaEvent.VIEW_MEDIA, onViewMediaDetail_handler);
			mediaManager.removeEventListener(MediaEvent.DELETE_MEDIAS, onDeleteMedia_handler);
			facade.removeMediator(NAME);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onCreateComplete_handler(e:FlexEvent):void
		{
			mediaManager.daterange = new DateRange().dateRange;
			mediaManager.removeEventListener(FlexEvent.CREATION_COMPLETE, onCreateComplete_handler);
			draftProxy.getAllMedia();
		}
		
		private function onDeleteMedia_handler(e:MediaEvent):void
		{
			draftProxy.deleteMedias(e.mediaList);
		}
		
		private function onEditMedia_handler(e:MediaEvent):void
		{
			e.stopPropagation();
			//TODO: Edit media title
		}
		
		private function onViewMediaDetail_handler(e:MediaEvent):void
		{
			e.stopPropagation();
			facade.sendNotification(ApplicationFacade.AIR_EXPLORER_OPEN, e.media.uri);
		}
		private var deleteMedia:MediaVO;
		private function onDeleteDraft_handler(e:MediaEvent):void
		{
			e.stopPropagation();
			deleteMedia = e.media;
			var cfm:UserConfirmCommandConfig = new UserConfirmCommandConfig(ReaderResource.instance.resource.gridDelete, ReaderResource.instance.resource.gridDeleteMediaConfirm, mediaManager, doDeleteMedia_handler);
			facade.sendNotification(ApplicationFacade.REQUEST_USERCONFIRM, cfm);
		}
		
		private function doDeleteMedia_handler():void
		{
			mediaManager.mediaDataGrid.removeItemAt(mediaManager.mediaDataGrid.getItemIndex(deleteMedia));
			mediaManager.mediaDataGrid.refresh();
			draftProxy.deleteMedia(deleteMedia);
			deleteMedia = null;
		}
		
	}
}